const reqHttps = require('axios-https-proxy-fix');
const reqHttp = require('axios');
const iconv = require('iconv-lite');
const config = require('../config.js');

async function request(url, isGbk) {
	let config = null;
	if (isGbk) {
		config = {
			url,
			responseType: 'arraybuffer',
			timeout: 10000,
		};
	} else {
		config = {
			url,
			timeout: 10000,
		};
	}

	// https和http分开
	let isHttps = url.search('https');
	if (isHttps !== -1) {
		return await reqHttps(config);
	} else {
		return await reqHttp(config);
	}
}
/**
 *
 * @param {*} url 传入路径
 * @param {*} source 小说来源
 * @param {*} params 参数
 * @returns {*} html
 */

exports.requestGet = async (url, source, params) => {
	let res = null;
	if (config[source].charset === 'gbk') {
		res = await request(url, true);
		return iconv.decode(res.data, config[source].charset);
	} else {
		res = await request(url, false);
		return res.data;
	}
};
